const blogDao = require("../dao/blogDao")
const tagDao = require("../dao/tagDao")
const tagBlogMapping = require('../dao/tagBlogMappingDao')
const timeUtil = require("../util/timeUtil")
const respUtil = require("../util/respUtil")
const url = require('url');
const path = new Map();

function queryBlogById(req, res) {
    const params = url.parse(req.url, true).query;
    blogDao.queryBlogById(parseInt(params.bid), function(result){
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "查询成功", result))
        res.end();
    })
    blogDao.addViews(parseInt(params.bid), function() {})
}
path.set("/queryBlogById", queryBlogById);

function queryHotBlog(req, res) {
    const params = url.parse(req.url, true).query;
    blogDao.queryHotBlog(5, function(result){
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "查询成功", result))
        res.end();
    })
}
path.set("/queryHotBlog", queryHotBlog);

function queryAllBlog(req, res) {
    blogDao.queryAllBlog(function(result){
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "查询成功", result))
        res.end();
    })
    
}
path.set("/queryAllBlog", queryAllBlog);

function queryBlogCount(req, res) {
    blogDao.queryBlogCount(function(result){
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "查询成功", result))
        res.end();
    })
}
path.set("/queryBlogCount", queryBlogCount);

function queryBlog(req, res) {
    var params = url.parse(req.url, true).query;
    blogDao.queryBlog(parseInt(params.page), parseInt(params.pageSize), function(result) {
        for (var i = 0; i < result.length; i ++) {
            result[i].content = result[i].content.replace(/<img[\w\W]*">/, "");
            result[i].content = result[i].content.replace(/<[\w\W]{1,5}>/g, "");
            result[i].content = result[i].content.substring(0, 300);
        }
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "查询成功", result))
        res.end();
    })
}
path.set("/queryBlog", queryBlog);

async function editBlog(req, res) {
    var params = url.parse(req.url, true).query;
    var tags = params.tag.replace(/ /g, "").replace("，", ",")
    req.on("data", await function(data) {
        console.log(data.toString())
        blogDao.insertBlog(params.title, data.toString(), tags, 0, timeUtil.getNow(), timeUtil.getNow(), function(result) {
            res.writeHead(200);
            res.write(respUtil.writeResult("success", "添加成功", result))
            res.end();
            var blogId = result.insertId;
            var tagList = tags.split(",");
            for(var i = 0; i < tagList.length; i ++) {
                if (tagList[i] == "") {
                    continue;
                }
                queryTag(tagList[i], blogId);
            }
        })
    })
}

path.set("/editBlog", editBlog);

function queryTag(tag, blogId) {
    tagDao.queryTag(tag, function (result) {
        if (result == null || result.length == 0) {
            insertTag(tag, blogId);
        } else {
            tagBlogMapping.insertTagBlogMapping(result[0].id, blogId, timeUtil.getNow(), timeUtil.getNow(), function (result) {

            })
        }
    })
}

function insertTag(tag, blogId) {
    tagDao.insertTag(tag, timeUtil.getNow(), timeUtil.getNow(), function (result) {
        insertTagBlogMapping(result.insertId, blogId);
    })
}

function insertTagBlogMapping(tagId, blogId) {
    tagBlogMapping.insertTagBlogMapping(tagId, blogId, timeUtil.getNow(), timeUtil.getNow(), function (result) {

    })
}






module.exports.path = path;